import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 计算器
 *
 */
public class Calculator extends JFrame implements ActionListener {
    JTextField x; //文本框
    String preOperater = ""; //上一次的运算符
    double sum = 0; //运算结果

    public Calculator(String title) {
        super(title);
        Container c = getContentPane(); //容器
        c.setLayout(new BorderLayout()); //容器采用边缘布局
        x = new JTextField(50);
        c.add("North", x);
        String[] buttoned = {"+/-", "sin", "cos", "C", "+", "x^y", "1", "2", "3", "=", "-", "！", "4", "5", "6", "0", "*", ".", "7", "8", "9", "←", "/"};
        JPanel p = new JPanel(); //面板
        p.setLayout(new GridLayout(4, 6, 10, 10));  //面板采用行列数4，6的网格布局
        for (int k = 0; k < buttoned.length; k++) { //用一个for循环将按钮添加进面板并注册监听器
            JButton b = new JButton(buttoned[k]);
            p.add(b);
            b.addActionListener(this);
        }
        c.add("Center", p);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //响应用户事件，根据点击按钮实现相关操作
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        try {
            if (Character.isDigit(command.charAt(0))) { //如果按钮事件为数字键
                x.setText(x.getText() + command); //添加数字
            } else if (command.equals(".")) { //如果按钮事件为"."键
                xiaoshu();
            } else if (command.equals("+/-")) { //如果按钮事件为"+/-"键
                fuhao();
            } else if (command.equals("←")) { //如果按钮事件为"退格"键
                backspace();
            } else if (command.equals("=")) {  //如果按钮事件为"="键
                dengyu();
            } else if (command.equals("cos")) { //如果按钮事件为"cos"键
                cos();
            } else if (command.equals("sin")) {  //如果按钮事件为"sin"键
                sin();
            } else if (command.equals("！")) {  //如果按钮事件为"阶乘"键
                fac();
            } else if (command.equals("C")) {  //如果按钮事件为"C"键
                clear();
            } else {
                doOperation();  //记录运算符和输入的数字
                x.setText("");
                preOperater = command;
            }
        } catch (Exception ex) {
            sum = 0;
            x.setText("");
        }
    }

    private void xiaoshu() {  //添加小数点
        String s = x.getText();
        if (!s.contains(".")) {
            x.setText(s + ".");
        }
    }

    private void fuhao() {  //添加负号
        String s = x.getText();
        if (!s.contains("-")) {
            x.setText("-" + x.getText());
        }
    }

    private void backspace() {  //退格
        String s = x.getText();
        if (s.length() > 0) {
            x.setText(s.substring(0, s.length() - 1)); //删除最后一个字符
        }
    }

    private void dengyu() {  //等号，显示计算结果
        doOperation();
        x.setText("" + sum);
        preOperater = "";
    }

    private void cos() {  //求该数的余弦
        double m = Double.parseDouble(x.getText());
        x.setText("" + Math.cos((m / 180) * Math.PI));
    }

    private void sin() {  //求该数的正弦
        double m = Double.parseDouble(x.getText());
        x.setText("" + Math.sin((m / 180) * Math.PI));
    }

    private void fac() {  //求阶乘
        double m = Double.parseDouble(x.getText());
        if (m <= 0) {
            x.setText("" + "必须为正整数!");//抛出不合理参数异常
        } else {
            int sum = 1;
            for (int i = 1; i <= m; i++) {//循环m
                sum *= i;//每循环一次进行乘法运算
            }
            x.setText("" + sum);//返回阶乘的值
        }
    }

    private void clear() {  //清零
        sum = 0;
        x.setText("");
        preOperater = "";
    }

    //根据记录的运算符将前面记录的结果sum与文本框中的数据进行运算
    private void doOperation() {
        double m = Double.parseDouble(x.getText());
        if (preOperater.equals("")) {
            sum = m;
        }
        //将结果与文本框中的数据按照前一个运算符运算
        if (preOperater.equals("+")) {
            sum = sum + m;  //加法运算
        }
        if (preOperater.equals("-")) {
            sum = sum - m;  //减法运算
        }
        if (preOperater.equals("*")) {
            sum = sum * m;  //乘法运算
        }
        if (preOperater.equals("/")) {
            sum = sum / m;  //除法运算
        }
        if (preOperater.equals("x^y")) {
            sum = Math.pow(sum, m); //求幂
        }
    }

    public static void main(String a[]) {
        JFrame x = new Calculator("计算器");
        x.setSize(400, 300);
        x.setVisible(true);
    }
}
